<?php
namespace app\admin\controller;
use think\Request;
use app\common\controller\Backend;
use app\common\model\BackendRuleModel;
use app\common\model\BackendMenuModel;

class Forbidden extends Backend
{
	public function index(Request $request = null){
  	$BackendRuleModel = new BackendRuleModel();
  	$list = $BackendRuleModel->getForbiddenList($request->get());

  	return view("index",[
  		'list' => $list,
  	]);
	}

	public function create(Request $request = null){
    if($request->isPost()){
      $post = $request->post();
      $BackendRuleModel = new BackendRuleModel();
      $result = $BackendRuleModel->allowField(true)->validate(true)->save($post);
      if(false === $result){
        // 验证失败 输出错误信息
        $this->error($BackendRuleModel->getError());
      }else{
        $this->success("成功！");
      }
    }else{
      //获取菜单
      $BackendMenuModel = new BackendMenuModel();
      $menuList = $BackendMenuModel->getMenuList();

      return view("create",['menuList'=>$menuList]);
    }
	}

  public function update(Request $request = null){
    if($request->isPost()){
      $post = $request->post();
      $BackendRuleModel = new BackendRuleModel();
      $result = $BackendRuleModel->allowField(true)->validate(true)->save($post,$post['id']);
      if(false === $result){
        // 验证失败 输出错误信息
        $this->error($BackendRuleModel->getError());
      }else{
        $this->success("成功！");
      }
    }else{
      $id = $request->param("id");
      //获取菜单
      $BackendMenuModel = new BackendMenuModel();
      $menuList = $BackendMenuModel->getMenuList();
      $BackendRuleModel = new BackendRuleModel();
      $ruleModel = $BackendRuleModel->getRuleById($id);

      return view("update",['menuList'=>$menuList,'ruleModel'=>$ruleModel]);
    }
  }

  /*删除*/
  public function deleteall(){

    $BackendRuleModel = new BackendRuleModel();
    if($BackendRuleModel->deleteAll($_POST['id']) > 0){
      return json(['status'=>200,'msg'=>'成功']);
    }else{
      return json(['status'=>400,'msg'=>'失败']);
    }
  }
}